package com.liugh.config;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.liugh.entity.standard.StandardDownload;
import com.liugh.enums.zhongche.DownloadEnum;
import com.liugh.mapper.StandardDownloadMapper;
import com.liugh.util.CommonUtil;
import com.liugh.util.FileUpload;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
public class SchedulTaskConfig {
    private static org.slf4j.Logger Logger = LoggerFactory.getLogger(SchedulTaskConfig.class);

    @Autowired
    StandardDownloadMapper standardDownloadMapper;

    @Scheduled(cron = "0 0 0 * * *")
    public void scheduleDelDownFile() {
        Date date = DateUtil.offsetDay(DateUtil.date(),-90);
        List<StandardDownload> downloads = standardDownloadMapper.selectList(new EntityWrapper<StandardDownload>().le("approve_time", date));
        for (StandardDownload download : downloads) {
            //删除FSTDFS中的文件
            Map<String,String> map = CommonUtil.getGroupAndName(download.getApplyDir());
            try {
                FileUpload.delFastDFSFile(map.get("group"), map.get("name"));

                Logger.info("删除远程文件 ： " + download.getApplyDir());
                //更新数据库中的状态为已过期
                download.setStatus(DownloadEnum.EXPIRED.getCode());
                download.setApplyDir("");
                standardDownloadMapper.updateById(download);
            } catch (Exception e) {
                Logger.error("删除fastDFS文件失败",e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
